let Compiler = require('./Compiler')
function webpack(options) {
  /* 
 1.初始化参数：从配置文件（webpack.config.js）和
 shell语句(命令行中参数或package中启动脚本配置的参数)中读取并合并参数，得出最终的配置对象
  */
 console.log(process.argv,'argv');
  let shellConfig = process.argv.slice(2).reduce((shellConfig, item) => {
    let [key, value] = item.split("="); // --mode=dev
    shellConfig[key.slice(2)] = value;
    return shellConfig
  }, {})
  let finalConfig = { ...options, ...shellConfig }
  // 2.用上一步得到的参数初始化compiler对象
  let compiler = new Compiler(finalConfig);
  // 3.加载配置项中的所有插件
  let { plugins } = finalConfig;
  for (let plugin of plugins) {
    plugin.apply(compiler)
  }

  return compiler
}
module.exports = webpack